//获取应用实例
const app = getApp();

//声明全局变量
let proListToTop = [], menuToTop = [], MENU = 0, windowHeight, timeoutId;
// MENU ==> 是否为点击左侧进行滚动的，如果是，则不需要再次设置左侧的激活状态
Page({
  data: {
    staticImg: app.globalData.staticImg,
    currentActiveIndex: 0,
    // 接口返回的商品数组
    navList: [],
    animate:'',
    goodsList:[],
    page:1,
    searchName:'',
  },
  onLoad: function (e) {
    // 确保页面数据已经刷新完毕~
    setTimeout(() => {
      // this.getAllRects()
    }, 20)
    console.log(e)
    this.setData({
      searchName:e.id
    })
    this.getAllList(e.id);
    wx.getStorage({
      key: 'token',
      success: function (res) {
        console.log(res)
        if (res.data != '') {
          app.globalData.token = res.data
        }
        // console.log("token=" + app.globalData.token)
      },
    })
  },


  getAllList(name){
    var that=this;
    console.log(that.data.searchName)
    wx.request({
      url: app.globalData.api + '/app.php?c=Haodanku&a=supersearch',
      data:
      {
        keyword: '' + that.data.searchName,
        back:10,
        is_coupon:'1',
        min_id: '' + that.data.page,
        tb_p:'1'
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
        console.log(res.data.data.length)
        // for (var i = 0; i < res.data.data.list.length; i++) {
        //   res.data.data.list[i].img = app.globalData.imgUrl + "" + res.data.data.list[i].img;
        //   that.setData({
        //     navList: res.data.data.list
        //   })
        // }
        // console.log(that.data.navList[0].cat_id)
           that.setData({
             navList: that.data.navList.concat(res.data.data)
          })
      }, fail: function () {

      }
    });
  },

  goodDetail:function(e){
    console.log(app.globalData.token)
    if(app.globalData.token!=''){
      console.log(e);
      wx.navigateTo({
        url: 'item?id='+e.currentTarget.dataset.id,
      })
    }else{
      wx.reLaunch({
        url: '/pages/mine/login',
      })
    }

    wx.request({
      url: app.globalData.api +'/app.php?c=Tbk&a=getGoodsMsg',
      data:
      {
        num_iid: e.currentTarget.dataset.id,
        token:app.globalData.token
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded",
        'platform': '' + app.globalData.platform,
        'version': '' + app.globalData.version,
      },
      method: 'POST',
      dataType: 'json',
      success: function (res) {
        console.log(res);
      }, fail: function () {

      }
    })
      
  },

  scroll(e) {
    // console.log(e);
    for (let i = 0; i < proListToTop.length; i++) {
      if (e.detail.scrollTop < proListToTop[i] && i !== 0 && e.detail.scrollTop > proListToTop[i - 1]) {
        return this.setDis(i)
      }
    }
    // 找不到匹配项，默认显示第一个数据
    if (!MENU) {
      this.setData({
        currentActiveIndex: 0
      })
    }
    MENU = 0
  },

  getActiveReacts() {
    wx.createSelectorQuery().selectAll('.menu-active').boundingClientRect(function (rects) {
      return rects[0].top
    }).exec()
  },
  getAllRects() {
    wx.createSelectorQuery().selectAll('.pro-item').boundingClientRect(function (rects) {
      rects.forEach(function (rect) {
        console.log(rect)
        // 这里减去44是根据你的滚动区域距离头部的高度，如果没有高度，可以将其删去
        proListToTop.push(rect.top - 44)
      })
    }).exec()
    // 获取menu数组的位置信息
    wx.createSelectorQuery().selectAll('.menu-item').boundingClientRect(function (rects) {
      wx.getSystemInfo({
        success: function (res) {
          // console.log(res);
          windowHeight = res.windowHeight / 2
          // console.log(windowHeight)
          rects.forEach(function (rect) {
            menuToTop.push({
              top: rect.top,
              // animate: rect.top > windowHeight
            })
          })
        }
      })
    }).exec()
  },
  // 获取系统的高度信息
  getSystemInfo() {
    let self = this
    wx.getSystemInfo({
      success: function (res) {
        windowHeight = res.windowHeight / 2
      }
    })
  },

  /**
 * 页面相关事件处理函数--监听用户下拉动作
 */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading(); //在标题栏中显示加载图标
    setTimeout(function () {
      wx.stopPullDownRefresh(); //停止加载
      wx.hideNavigationBarLoading(); //隐藏加载icon
    }, 2000)
    this.setData({
      page:1,
      navList:[]
    })
    that.getAllList();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // this.getCartList()
    var that=this;
    that.setData({
      page: that.data.page+1
    })
    that.getAllList();
  },


})